public class Demo18 {
    static class  Counter{
        private static Object calss;
        private int count=0;
        private Object locker = new Object();
        //第一种写法
        public void add(){
            synchronized(locker){
            count++;
            }
        }
        //第二种写法
        // synchronized void  add(){
        //     count++;
        // }
        synchronized public void func(){

        }
        public void func2(){
            synchronized(Counter.calss){

            }
        }
    }
    public static void main(String[] args) {
        Counter counter =new Counter();
        Thread t1=new Thread(()->{
            for(int i=0;i<50000;i++){
                counter.add();
            }
        });
        Thread t2=new Thread(()->{
            for(int i=0;i<50000;i++){
                counter.add();
            }
        });
        t1.start();
        t2.start();
        try {
            t1.join();
            t2.join();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println(counter.count);
    }

}
